package com.yzk.auction.controller;

import com.yzk.auction.entity.Bidding;
import com.yzk.auction.entity.GoodsDTO;
import com.yzk.auction.service.BiddingService;
import com.yzk.auction.service.GoodsService;
import com.yzk.auction.service.OrderSerivce;
import com.yzk.auction.utils.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "homePage",urlPatterns = "/homePage")
public class HomePageController extends HttpServlet {
    private GoodsService goodsService = new GoodsService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    String second_name;
    String like;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if (method.equals("homepage_list")) {
            this.homePageList(request, response);
        } else if (method.equals("comm_datils")) {
            this.commDatils(request,response);
        } else if(method.equals("homePage_like")){   //根据分类id查询商品，分页显示
            this.homePage_like(request,response);
        } else if(method.equals("homePage_id")){   //根据分类id查询商品，分页显示
            this.homepage_id(request,response);
        } else if (method.equals("comm_datils1")) {
            this.commDatils1(request,response);
        } else if (method.equals("creat_bidding")) {
            this.creatBidding(request,response);
        }

    }

    private void creatBidding(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String good_id = request.getParameter("good_id");
            System.out.println("good_id:"+good_id);

            String bidding_time = request.getParameter("bidding_time");
            System.out.println("bidding_time:"+bidding_time);

            String user_id = request.getParameter("user_id");
            System.out.println("user_id:"+user_id);

            Double bidding_price = Double.parseDouble(request.getParameter("bidding_price"));
            System.out.println("bidding_price:"+bidding_price);

            int num = Integer.parseInt(request.getParameter("num"));
            System.out.println(num);
            num++;

            List<Bidding> bid_list = BiddingService.listGoods(Integer.parseInt(good_id));
            bidding_price = bidding_price+bid_list.get(0).getBidding_price();

            int bidding_status = 1;
            long count = BiddingService.count();
            count++;

            Date date = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss").parse(bidding_time);
            //修改状态（领先，出局）
            BiddingService biddingService = new BiddingService();
            biddingService.updateStatus(Integer.parseInt(good_id));

            //创建新的订单
            BiddingService.insert(count,bidding_status,Integer.parseInt(user_id),bidding_price,date,Integer.parseInt(good_id));


            List<GoodsDTO> list =GoodsService.listGoods();
            int id = Integer.parseInt(good_id);
            GoodsDTO goodsDTO = null;
            for (GoodsDTO goods:list
            ) {
                if(id==goods.getGoods_id()){
                    goodsDTO=goods;
                }
            }
            if(goodsDTO.getAuction_times()==0){
                /*order新增*/
                OrderSerivce.insertOrsers(Integer.parseInt(good_id),Integer.parseInt(user_id),date,0);
            }else{
                /*order修改*/
                OrderSerivce.updateOrsers(Integer.parseInt(good_id),Integer.parseInt(user_id),date,0);
            }

            //更新商品信息
            goodsService.undateGoodsNum(num,bidding_price,Integer.parseInt(good_id));

            /*将数据添加到bidding表*/
            /*跳转到商品详情界面*/
            //commDatils(request,response);
            request.getRequestDispatcher("homepage.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void homePage_like(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String p = request.getParameter("p");
        String r = request.getParameter("r");
        System.out.println(p);
        if(p==null) {
            like = request.getParameter("like");
        }
        //GoodsDao goodsDao = new GoodsDao();
        if(p == null) {
            p = "1";
        }
        if(r == null) {
            r = "8";
        }
        try {
            PageModel paginationlike = goodsService.paginationlike(like,Integer.parseInt(p),Integer.parseInt(r));
            request.setAttribute("pageModel1", paginationlike);
            request.getRequestDispatcher("WEB-INF/jsp/selectpage.jsp").forward(request, response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    protected void homePageList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String p = request.getParameter("p");
        String r = request.getParameter("r");
        if (p == null) {
            p = "1";
        }
        if (r == null) {
            r = "3";
        }
        PageModel pageModel = goodsService.pagination(Integer.parseInt(p), Integer.parseInt(r));
        request.setAttribute("pageModelMenu", pageModel);
        request.getRequestDispatcher("WEB-INF/jsp/homepage_list.jsp").forward(request, response);
    }

    protected void commDatils(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String goods_id = request.getParameter("goods_id");
        List<GoodsDTO> list =GoodsService.listGoods();
        int id = Integer.parseInt(goods_id);
        GoodsDTO goodsDTO = null;
        for (GoodsDTO goods:list
        ) {
            if(id==goods.getGoods_id()){
                goodsDTO=goods;
            }
        }
        List<Bidding> bid_list = BiddingService.listGoods(id);
        request.setAttribute("goods",goodsDTO);
        request.setAttribute("bidding",bid_list);
        request.getRequestDispatcher("comm_datils.jsp").forward(request,response);
    }

    protected void commDatils1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String goods_id = request.getParameter("goods_id");
        List<GoodsDTO> list =GoodsService.listGoods();

        int user_id=1001;
        int id = Integer.parseInt(goods_id);

        GoodsDTO goodsDTO = null;
        for (GoodsDTO goods:list
        ) {
            if(id==goods.getGoods_id()){
                goodsDTO=goods;
            }
        }

        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
        String now = sdf.format(new Date());
        long count = OrderSerivce.ordersCount();
        request.setAttribute("goods_now",goodsDTO);
        request.setAttribute("order_time",now);
        request.setAttribute("order_id",count);
        request.setAttribute("user_id",user_id);

        request.getRequestDispatcher("订单.jsp").forward(request,response);
    }

    /**
     * //根据分类id查询商品，分页显示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void homepage_id(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String p = request.getParameter("p");
        String r = request.getParameter("r");
        if (p==null) {
            second_name = request.getParameter("second_name");
        }

        if(p == null) {
            p = "1";
        }
        if(r == null) {
            r = "8";
        }

        //根据二级分类名查询
        try {
            PageModel pageModel1 = goodsService.paginationsecondid(second_name,Integer.parseInt(p),Integer.parseInt(r));
            request.setAttribute("pageModel1", pageModel1);
            request.getRequestDispatcher("WEB-INF/jsp/sortpage.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
